package com.wen.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.wen.common.Result;
import com.wen.entity.MemberOrder;
import com.wen.service.MemberOrderService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author dhh
 * @date 2021/11/21 16:29
 * 预约课程
 */
@RestController
@AllArgsConstructor
@RequestMapping("/memberOrder")
public class MemberOrderController {

    private final MemberOrderService memberOrderService;

    @GetMapping("/getMemberOrder")
    public Result<?> getMemberOrder(@RequestParam Integer memberId, @RequestParam String search) {
        List<Map<String, Object>> mapList = memberOrderService.getMemberOrder(memberId, search);
        for (Map<String, Object> stringObjectMap : mapList) {
            Result result = this.getMember(Integer.parseInt(stringObjectMap.get("memberId").toString()), Integer.parseInt(stringObjectMap.get("courseId").toString()));
            List<MemberOrder> memberOrders = (List<MemberOrder>) result.getData();
            if (memberOrders.size() > 0) {
                stringObjectMap.put("orderStatus", memberOrders.get(0).getOrderStatus());
                stringObjectMap.put("orderId", memberOrders.get(0).getId());
            } else {
                stringObjectMap.put("orderStatus", "0");
            }
        }
        return Result.success(mapList);
    }

    @PutMapping
    public Result<?> update(@RequestBody MemberOrder memberOrder) {
        memberOrderService.updateById(memberOrder);
        return Result.success();
    }

    //    添加信息
    @PostMapping
    public Result<?> save(@RequestBody MemberOrder memberOrder) {
        memberOrderService.save(memberOrder);
        return Result.success();
    }

    @GetMapping("/getOrder")
    public Result<?> getMember(@RequestParam Integer memberId, @RequestParam Integer courseId) {
        LambdaQueryWrapper<MemberOrder> wrapper = Wrappers.<MemberOrder>lambdaQuery();
        wrapper.eq(MemberOrder::getMemberId, memberId);
        wrapper.eq(MemberOrder::getCourseId, courseId);
        List<MemberOrder> memberOrders = memberOrderService.list(wrapper);
        return Result.success(memberOrders);
    }
}
